package com.example.controller;

import com.example.pojo.dto.PageDTO;
import com.example.pojo.dto.ProductInventoryDTO;
import com.example.pojo.dto.SaveProductAllotApplyDTO;
import com.example.pojo.dto.UpdateProductAllotApplyDTO;
import com.example.pojo.orm.ProductInventory;
import com.example.pojo.vo.PageVO;
import com.example.service.impl.ProductInventoryServiceImpl;
import com.example.util.JsonResult;
import com.example.util.ResultCodeEM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api("季鑫利:商品库存controller")
public class ProductInventoryController {

    /**
     * 日志
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 商品库存接口
     */
    @Autowired
    private ProductInventoryServiceImpl productInventoryService;

    @PostMapping("saveProductInventory")
    @ApiOperation("季鑫利:新增商品库存信息")
    public JsonResult saveProductInventory(@ApiParam("新增商品库存参数实体") SaveProductAllotApplyDTO saveProductAllotApplyDTO) {
        JsonResult result;
        try {
            result = productInventoryService.saveProductInventory(saveProductAllotApplyDTO);
        } catch (Exception e) {
            log.error("新增商品库存异常：--->{}", e.getMessage());
            result=new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("新增商品库存异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("updateProductInventory")
    @ApiOperation("季鑫利:更新商品库存信息")
    public JsonResult saveProductInventory(@ApiParam("更新商品库存参数实体") UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result;
        try {
            result = productInventoryService.updateProductInventory(updateProductAllotApplyDTO);
        } catch (Exception e) {
            log.error("更新商品库存异常：--->{}", e.getMessage());
            result=new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("更新商品库存异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("removeProductInventory")
    @ApiOperation("季鑫利:逻辑删除商品库存")
    public JsonResult removeProductInventory(@ApiParam("商品类别名称") @RequestParam Integer id) {
        JsonResult result;
        try {
            result = productInventoryService.removeProductInventory(id);
        } catch (Exception e) {
            result = new JsonResult();
            log.error("逻辑删除商品类别异常：--->{}", e.getMessage());
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("逻辑删除商品库存异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @GetMapping("queryProductInventory")
    @ApiOperation("季鑫利:查询商品库存")
    public JsonResult<PageVO<ProductInventory>> queryProductInventory(PageDTO pageDTO) {
        return productInventoryService.queryProductInventory(pageDTO);
    }

    @GetMapping("queryProductInventoryById")
    @ApiOperation("季鑫利:根据商品id查询库存")
    public JsonResult<ProductInventory> queryProductInventoryById(@ApiParam("商品id") @RequestParam Long id) {
        JsonResult result;
        try {
            result = productInventoryService.queryProductInventoryById(id);
        } catch (Exception e) {
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setData(e.getMessage());
        }
        return result;
    }
}
